<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>关注消息管理</title>
	<meta name="decorator" content="default"/>
</head>
<body>
<div class="main-content">
    <tags:breadcrumb menuId="29d6bcf64a8c463885718e18ac1d51d5" />
    <div class="page-content">
        <div class="widget-box">
            <div class="widget-header">
                <h5 class="widget-title bigger lighter">
                    <i class="ace-icon fa fa-table"></i>文本消息模板
                </h5>
                <div class="widget-toolbar">
                    <a href="javascript:;" class="btn btn-primary" id="addInfo">添加</a>
                </div>
            </div>

            <div class="widget-body">
                <form:form id="searchForm" modelAttribute="subscribe" action="${ctx}/wechat/subscribe/" method="post" class="form-horizontal">
                    <input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
                    <input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
                </form:form>
                <table id="contentTable" class="table table-striped table-bordered table-hover">
                    <thead>
                    <tr>
                        <th>模板名称</th>
                        <th>微信公众号</th>
                        <th>模板类型</th>
                        <th>添加时间</th>
                        <shiro:hasPermission name="wechat:subscribe:view">
                            <th>操作</th>
                        </shiro:hasPermission></tr>
                    </thead>
                    <tbody>
                    <c:forEach items="${page.list}" var="subscribe">
                        <tr>
                            <td><a href="${ctx}/wechat/subscribe/form?id=${subscribe.id}">${subscribe.templatename}</a></td>
                            <td accountid="${subscribe.accountid}">${subscribe.accountname}</td>
                            <td>${subscribe.msgtype}</td>
                            <td>${subscribe.addtime}</td>
                            <shiro:hasPermission name="wechat:subscribe:view"><td>
                                <a href="javascript:;" onclick="update('${subscribe.id}');">修改</a>
                                <a href="javascript:;" onclick="deleteMsg('确认要删除该关注消息吗？','${ctx}/wechat/subscribe/delete?id=${subscribe.id}',this);">删除</a>
                            </td></shiro:hasPermission>
                        </tr>
                    </c:forEach>
                    </tbody>
                </table>
            </div>
        </div>
        <div class="pagination">${page}</div>
    </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">添加文本消息模板</h4>
            </div>
            <div class="modal-body">
                <form:form id="inputForm" modelAttribute="subscribe" action="" method="post" class="form-horizontal">
                    <form:hidden path="id" />
                    <form:hidden path="templatename" />
                    <div class="form-group">
                        <label class="col-sm-2 no-padding-right control-label">公众号:</label>
                        <div class="col-sm-8">
                            <form:select path="accountid" cssClass="required col-sm-8">
                                <form:option value="">---请选择---</form:option>
                                <form:options items="${accountList}" itemValue="id" itemLabel="accountName"/>
                            </form:select>
                            <span class="important">*</span>
                        </div>
                    </div><hr>
                    <div class="form-group">
                        <label class="col-sm-2 no-padding-right control-label">消息类型:</label>
                        <div class="col-sm-8">
                            <form:select path="msgtype">
                                <form:option value="">---请选择---</form:option>
                                <form:options items="${fns:getDictList('msg_type')}" itemValue="value" itemLabel="label" />
                            </form:select>
                            <span class="important">*</span>
                        </div>
                    </div><hr>
                    <div class="form-group">
                        <label class="col-sm-2 no-padding-right control-label">消息模板:</label>
                        <div class="col-sm-8">
                            <form:select path="templateid" cssClass="required col-sm-8">
                                <form:option value="">---请选择---</form:option>
                                <form:options items="${templateList}" itemValue="id" itemLabel="templatename"/>
                            </form:select>
                            <span class="important">*</span>
                        </div>
                    </div><hr>
                </form:form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="btnSubmit">确定</button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function() {
        $("#addInfo").click(function(){
            $("#myModal").modal();
        });
        //确定
        $("#btnSubmit").click(function(){
            if($("#inputForm").valid()){
                $("#templatename").val($("#templateid").find("option:selected").text());
                $.post("${ctx}/wechat/subscribe/save",$("#inputForm").serialize(),function(data){
                    if(data.success){
                        showNote("success", function () {
                            window.location.href = "${ctx}/wechat/subscribe/list";
                        }, data.msg);
                    }else {
                        showNote("error", function () {
                            return false;
                        }, data.msg);
                    }
                });
            }else{
                showNote("error", function () {
                    return false;
                }, "请输入正确的信息");
            }
        });
    });
    function page(n,s){
        $("#pageNo").val(n);
        $("#pageSize").val(s);
        $("#searchForm").submit();
        return false;
    }

    //更新产品信息
    function update(id){
        var url = "${ctx}/wechat/subscribe/find";
        $.post(url,{id:id},function(data){
            $("#id").val(data.id);
            $("#templatename").val(data.templatename);
            $("#accountid").find("option[value='"+data.accountid+"']").attr("selected",true);
            $("#msgtype").find("option[value='"+data.msgtype+"']").attr("selected",true);
            $("#templateid").find("option[value='"+data.templateid+"']").attr("selected",true);
        });
        $("#myModal").modal();
    }

    //删除
    function deleteMsg(msg,href,tr){
        layer.confirm(msg,function(index){
            layer.close(index);
            loading();
            $.post(href,function(data){
                if(data.success){
                    showNote("success", function () {
                        $(tr).parent().parent().remove();
                    }, data.msg, "center");
                }else {
                    showNote("error", function () {
                        return false;
                    }, data.msg);
                }
            });
        });
    }
</script>
</body>
</html>
